package com.example.administrator.liveapp.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.MainActivity;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.bean.AdvertisingBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.ViewInjector;
import org.xutils.view.annotation.ViewInject;

/**
 * 欢迎页activity
 */

public class TransitionPage extends AppCompatActivity implements View.OnClickListener{

    private ImageView image;
    private TextView text;
    AdvertisingBean bean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉Activity上面的状态栏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.activity_transition_page);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        image = (ImageView) findViewById(R.id.image);
        text = (TextView) findViewById(R.id.text);
        initData();
        text.setOnClickListener(this);
    }

    private void initData() {
        String url = AppUrl.URL + AppUrl.advertising;
        XUtils.Get(url,null,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                bean = gson.fromJson(result,AdvertisingBean.class);
//                Glide.with(TransitionPage.this).load(AppUrl.URL+bean.getTrans().get(0)).into(image);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                timer.start();
            }
        });
    }

    /**
     * 倒计时:
     */
    private CountDownTimer timer = new CountDownTimer(2000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            text.setText((millisUntilFinished / 1000) + "跳过");
        }
        @Override
        public void onFinish() {
            Intent intent = new Intent(TransitionPage.this,MainActivity.class);
            startActivity(intent);
            finish();
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.text:
                timer.cancel();
                Intent intent = new Intent(TransitionPage.this,MainActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}
